// pages/bureau/bureau.js
// const request = require('../../utils/request.js');
import { picUrl, formatTime,dataHandleNull } from '../../utils/util.js';
import { conventionDetail } from '../../utils/getData.js'
const getopen = require('../../utils/maxutil.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    minModel:app.globalData.minModel, //小机型iphone8
    
    headerStatus:3, //1:箭头分离头像,2箭头头像,3头像关闭图标 头部状态
    avatarstatus:1,
    conventionId:'',
    conventiondata:'', //
     isloding:false, //加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let conventionId = '';
    //扫码进来
    if(options.scene && options.scene){ 
      conventionId = options.scene
    }
    //页面内跳入
    if(options.conventionId && options.conventionId){
      conventionId = options.conventionId;
    }
    //是否登录
    if(wx.getStorageSync('userId')){
      this.getdetail(conventionId);
    }else{
      this.login(conventionId);
    }
  },
  //登录
  login(conventionId){
    let than = this;
    getopen.loginOpenid().then(res=> {
      console.log('取到了openid')
      this.getdetail(conventionId);
    })
  },

  getdetail(conventionId){
    let than = this;
   let data = {
     userId:wx.getStorageSync('userId'),
     conventionId:conventionId,
   }

   conventionDetail(data).then( function (res){
     console.log(res)
     if(res.success){
       let convention = dataHandleNull(res.result)  
       let data = {
         ...convention,
         conventionCoverPage:picUrl('activity/img/',convention.conventionCoverPage),
         conventionBlurCoverPage:picUrl('activity/img/',convention.conventionBlurCoverPage),
         detailImg:picUrl('activity/img/',convention.detailImg),
         shareImg:picUrl('activity/img/',convention.shareImg),
         avatarstatus:convention.status,
         conventionDate:convention.conventionDate.slice(5).replace("-","/"),
        //  conventionUserMap:convention.conventionUserMap.participantlist.map((item)=>{
        //    return {...item, avatarPath:picUrl('',item.avatarPath), userPic:picUrl('',item.userPic)}
        //  }),
         sponsorUser:{...convention.sponsorUser,avatarPath:picUrl('',convention.sponsorUser.avatarPath)}
       }
       wx.setStorageSync('lookCoverPage', data.conventionCoverPage); //在看的主题图片(局或者活动)，登录流程需要用到

       //判断活动是否结束
       let currentTime = formatTime(new Date()); //当前时间
       currentTime = currentTime.substring(0,currentTime.length-3);
       let conventionEndTime = convention.conventionDate.replace(/-/g,"/") +' '+ convention.conventionEndTime  //活动结束时间
       console.log('活动结束时间')
       console.log(currentTime >= conventionEndTime)

       
          //传给头部headers组件的数据
          let headerdata = {
            conventionId:data.conventionId,
            // sponsorId:convention.sponsorId,
            // avatarPath:picUrl('',convention.sponsorUser.avatarPath),
            // userName:convention.sponsorUser.userName,
            islike:data.guanzhu,
            shareImg:data.shareImg
          }
          //局汇总需要的基础数据，避免重复打接口
          let conventioninfo = {
            avatarstatus:data.avatarstatus,
            conventionName:data.conventionName,
            conventionDate:data.conventionDate,
            conventionBeginTime:data.conventionBeginTime,
            conventionEndTime:data.conventionEndTime,
            conventionAddress:data.conventionAddress,
            conventionCoverPage:data.conventionCoverPage,
            conventionBlurCoverPage:data.conventionBlurCoverPage,
            conventionImageRGB:data.conventionImageRGB,
            conventionInvalidation:currentTime >= conventionEndTime, //活动是否失效
            // conventionInvalidation:false, //未结束
            headerdata
          }
          app.globalData.conventionInfo = conventioninfo

          than.setData({
            conventiondata:data,
            isloding:true,
            // headerdata,
          })
     }
   })
 },
 _maxCardClose(){
   console.log('页面回调了')
 },


  /**
   * 用户点击分享
   */
  onShareAppMessage: function () {
    return {
      path: 'pages/bureau/bureau?conventionId='+this.data.conventiondata.conventionId,
      imageUrl: this.data.conventiondata.shareImg,
    };
    // 用户点击右上角分享给好友,要先在分享好友这里设置menus的两个参数,才可以分享朋友圈
    wx.showShareMenu({
     withShareTicket: true,
     menus: ['shareAppMessage', 'shareTimeline']
   })
  },

  //用户点击右上角分享朋友圈
  onShareTimeline: function () { 
    return {
        path: 'pages/bureau/bureau?conventionId='+this.data.conventiondata.conventionId,
        imageUrl: this.data.list[this.data.current].shareImg,
      }
  },
  
})